package com.example.text.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.text.common.result.Result;
import com.example.text.common.result.ResultFactory;
import com.example.text.entity.AdminRolePermission;
import com.example.text.entity.ExamineEntity;
import com.example.text.entity.FuleassociationEntity;
import com.example.text.service.ExamineService;
import com.example.text.service.FuleassociationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
public class BidEvaluationController {
    @Autowired
    ExamineService examineService;

    @Autowired
    FuleassociationService fuleassociationService;
    //显示中标审批表
    @RequiresPermissions("/api/admin/implement/purchases")
    @GetMapping("/api/admin/implement/purchases/{size}/{page}")
    public Result listPurchase(@PathVariable("size") int size, @PathVariable("page") int page) throws Exception {
        System.out.println("一页有："+size+"个数据");
        System.out.println("从第"+page+"个页开始");
        Page<ExamineEntity> pages = new Page<>(page,size);
        return ResultFactory.buildSuccessResult(examineService.selectExaminePage(pages,3));
    }

    //根据编号就显示中标审批表
    @RequiresPermissions("/api/admin/implement/purchases")
    @GetMapping("/api/admin/implement/purchases/{size}/{page}/{id}")
    public Result listPurchaseByWord(@PathVariable("size") int size, @PathVariable("page") int page,
                                     @PathVariable("id") String id) throws Exception {
        System.out.println("一页有："+size+"个数据"+"id:"+id);
        System.out.println("从第"+page+"个页开始");
        Page<ExamineEntity> pages = new Page<>(page,size);
        if(examineService.selectExaminePagebyWord(pages,id,3)==null){
            return ResultFactory.buildFailResult("没有该编号，请重新输入");
        }else {
            return ResultFactory.buildSuccessResult(examineService.selectExaminePagebyWord(pages, id,3));
        }
    }

    //根据状态就显示采购审批表
    @RequiresPermissions("/api/admin/implement/purchases")
    @GetMapping("/api/admin/implement/purchases/{size}/{page}/stat/{status}")
    public Result listPurchaseByStatus(@PathVariable("size") int size, @PathVariable("page") int page,
                                       @PathVariable("status") int stutas) throws Exception {
        System.out.println("一页有："+size+"个数据"+"id:"+stutas);
        System.out.println("从第"+page+"个页开始");
        Page<ExamineEntity> pages = new Page<>(page,size);
        return ResultFactory.buildSuccessResult(examineService.selectExaminePagebyStatus(pages, stutas,3));
    }
    //删除中标审批表
    @PostMapping("/api/admin/implement/purchases/delete")
    public Result delete(@RequestBody ExamineEntity examineEntity) throws Exception {
        //修改上一级的任务：中标下达
        //先获取上一级的数据
        ExamineEntity entityDB=examineService.getById(examineEntity.getId());
        int fuleapplyId=entityDB.getFuleapplyid();
        int audit_status=2;
        int status=2;
        //查询上一级数据
        ExamineEntity last= examineService.lastExamine(fuleapplyId,audit_status,status);
        //修改上一级状态
        last.setRemark("");
        last.setAuditStatus(1);
        last.setCreateTime(null);
        //删除中标结果：
        fuleassociationService.remove(new QueryWrapper<FuleassociationEntity>().eq("fuleapplyid",fuleapplyId));

        examineService.updateById(last);
        examineService.removeById(examineEntity.getId());
        return ResultFactory.buildSuccessResult("删除成功");
    }
    //批量删除中标审核表
    @PostMapping("/api/admin/implement/purchases/deletes/{ids}")
    public Result deletePurchase(@PathVariable("ids") String ids){
        System.out.println("ids"+ids);
        String[] AdvertiseIds = ids.split("\\,");
        examineService.removeByIds(Arrays.asList(AdvertiseIds));
        return ResultFactory.buildSuccessResult("批量采购审核表删除成功");
    }

    //中标表审核
    @CrossOrigin
    @PutMapping("/api/admin/implement/purchases/status/{username}")
    public Result updateAdvertiseStatus(@RequestBody ExamineEntity requestExamine,@PathVariable("username") String username ) {
        System.out.println("传入的值为："+requestExamine+"执行者为："+username);
        System.out.println("执行者为："+username);
        ExamineEntity examineEntity=examineService.updateImplenmentExamineStatus(requestExamine,username);
        String message = "审核成功";
        return ResultFactory.buildSuccessResult(message);
    }
}
